package com.jstide.credit.data.service.biz;

import cn.hutool.core.util.StrUtil;
import com.jstide.credit.data.service.domain.SecondCategory;
import com.jstide.credit.data.service.domain.TopCategory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author SangLiang on 2019-08-22.
 * @version 1.0
 */

public class CalcGather {

    private static final Logger logger = LoggerFactory.getLogger(CalcGather.class);

    private static final List<TopCategory>  TOP_CATEGORY_LIST = new ArrayList(){{
        add(new TopCategory("基础信息",200,150,Stream.of("基本信息","婚姻信息","教育信息").collect(Collectors.toList())));
        add(new TopCategory("身份信息",100,0,Stream.of("法人代表","董监高","个体工商户","驾驶员","其他身份信息").collect(Collectors.toList())));
        add(new TopCategory("职业信息",80,0,Stream.of("就业信息","职业资格","重点信用人群").collect(Collectors.toList())));
        add(new TopCategory("良好信息",100,30,Stream.of("表彰信息","行政奖励信息").collect(Collectors.toList())));
        add(new TopCategory("不良信息",0,120,Stream.of("刑事判决","被执行人","黑名单","行政处罚","其他失信信息","执纪问责信息").collect(Collectors.toList())));
        add(new TopCategory("文明行为信息",0,50,Stream.of("文明行为信息").collect(Collectors.toList())));
        add(new TopCategory("履约信息",0,100,Stream.of("欠费信息").collect(Collectors.toList())));
        add(new TopCategory("公益信息",50,0,Stream.of("公益信息").collect(Collectors.toList())));
    }};

    private static final List<SecondCategory>  SECOND_CATEGORY_LIST = new ArrayList(){{
        add(new SecondCategory("基本信息",150, Stream.of("3","4").collect(Collectors.toList())));
        add(new SecondCategory("婚姻信息",10, Stream.of("5").collect(Collectors.toList())));
        add(new SecondCategory("教育信息",40, Stream.of("6_7").collect(Collectors.toList())));
        add(new SecondCategory("法人代表",50, Stream.of("8").collect(Collectors.toList())));
        add(new SecondCategory("董监高",40, Stream.of("9").collect(Collectors.toList())));
        add(new SecondCategory("个体工商户",30, Stream.of("10").collect(Collectors.toList())));
        add(new SecondCategory("驾驶员",20, Stream.of("11").collect(Collectors.toList())));
        add(new SecondCategory("其他身份信息",60, Stream.of("12").collect(Collectors.toList())));
        add(new SecondCategory("就业信息",10, Stream.of("13").collect(Collectors.toList())));
        add(new SecondCategory("职业资格",20, Stream.of("14","15","16").collect(Collectors.toList())));
        add(new SecondCategory("重点信用人群",50, Stream.of("17","18","19","20").collect(Collectors.toList())));
        add(new SecondCategory("表彰信息",100, Stream.of("21").collect(Collectors.toList())));
        add(new SecondCategory("行政奖励信息",50, Stream.of("22").collect(Collectors.toList())));
        add(new SecondCategory("刑事判决",-120, Stream.of("23").collect(Collectors.toList())));
        add(new SecondCategory("被执行人",-100, Stream.of("24").collect(Collectors.toList())));
        add(new SecondCategory("黑名单",-100, Stream.of("25").collect(Collectors.toList())));
        add(new SecondCategory("行政处罚",-80, Stream.of("26").collect(Collectors.toList())));
        add(new SecondCategory("其他失信信息",-80, Stream.of("27","28").collect(Collectors.toList())));
        add(new SecondCategory("执纪问责信息",-120, Stream.of("29","30").collect(Collectors.toList())));
        add(new SecondCategory("文明行为信息",-50, Stream.of("31").collect(Collectors.toList())));
        add(new SecondCategory("欠费信息",-100, Stream.of("32").collect(Collectors.toList())));
        add(new SecondCategory("公益信息",100, Stream.of("33","34").collect(Collectors.toList())));
    }};



    public static Map<String,Integer> getSecondCategoryScore(  Map<String , Integer> itemScoreList ){
        Map<String,Integer> result = new LinkedHashMap(30);
        SECOND_CATEGORY_LIST.stream().forEach(t->{
            try {
                int extValue = t.getExtValue();
                int itemTotal = t.getIndicators().stream().map(s -> itemScoreList.get(s)).reduce(0, Integer::sum);
                if (extValue >= 0) {
                    itemTotal = itemTotal > extValue ? extValue : itemTotal;
                } else {
                    itemTotal = itemTotal < extValue ? extValue : itemTotal;
                }
                result.put(t.getName(), itemTotal);
            }
            catch (Exception e){
                logger.error(StrUtil.format("- second category error:{} -",t),e);
            }
        });
        return result;
    }

    public static Map<String,Integer> getTopCategoryScore(  Map<String , Integer>  scoreList ){
        Map<String,Integer> result = new LinkedHashMap(8);
        TOP_CATEGORY_LIST.stream().forEach(t->{
            try {
                int extValue = t.getExtValue();
                int itemTotal = t.getIndicators().stream().map(s -> scoreList.get(s)).reduce(t.getDefaultScore(), Integer::sum);
                if (extValue > 0) {
                    itemTotal = itemTotal > extValue ? extValue : itemTotal;
                } else {
                    itemTotal = itemTotal < extValue ? extValue : itemTotal;
                }

                result.put(t.getName(), itemTotal);
            }catch (Exception e){
                logger.error(StrUtil.format("- top category error:{} -",t),e);
            }
        });

        return result;
    }


    public static int getFinalScore( Map<String,Integer> topCategoryScoreMap){
        int result = topCategoryScoreMap.values().stream().reduce(200,Integer :: sum);
        result = result>1000?1000:result;
        result = result<0?0:result;
        return result;
    }



}
